#include"newdownloadwindow.h"
#include<QGridLayout>
#include<QPushButton>
#include<QLabel>
#include<QSlider>
#include<QLineEdit>
#include<QFileDialog>


NewDownloadWindow::NewDownloadWindow(QWidget *parent, const QString& folder):QDialog(parent)
{
    this->setModal(true);
    this->setWindowTitle("Download properties");
    this->setFixedSize(600, 200);

    QGridLayout * layout = new QGridLayout();
    this->setLayout(layout);
    layout->setRowMinimumHeight(0, 50);
    layout->setRowMinimumHeight(1, 50);
    layout->setRowMinimumHeight(2, 50);
    layout->setRowMinimumHeight(3, 50);

    urlLineEdit = new QLineEdit();
    directoryEdit = new QLineEdit();
    QPushButton * directoryButton = new QPushButton("Select");
    QPushButton * downloadButton = new QPushButton("Download");
    QPushButton * rejectButton = new QPushButton("Cancel");

    prioritySlider = new QSlider();
    PriorityLabel * priorityLabel = new PriorityLabel();

    prioritySlider->setOrientation(Qt::Horizontal);
    prioritySlider->setRange(0,2);
    prioritySlider->setFocusPolicy(Qt::StrongFocus);

    directoryButton->setFocusPolicy(Qt::StrongFocus);
    downloadButton->setFocusPolicy(Qt::StrongFocus);
    rejectButton->setFocusPolicy(Qt::StrongFocus);

    layout->addWidget(new QLabel(("URL:")), 0, 0);
    layout->addWidget(urlLineEdit, 0, 1, 1, -1);

    layout->addWidget(new QLabel(("Folder to save in:")), 1, 0);
    layout->addWidget(directoryEdit, 1, 1, 1, 4);
    layout->addWidget(directoryButton, 1, 5);

    layout->addWidget(new QLabel(("Priority:")), 2, 0);
    layout->addWidget(prioritySlider, 2, 1, 1, 4);
    layout->addWidget(priorityLabel, 2, 5);

    dialog = new QFileDialog(this);
    dialog->setModal(true);
    dialog->setFileMode(QFileDialog::DirectoryOnly);

    connect(directoryButton, SIGNAL(clicked()), dialog, SLOT(open()));
    connect(dialog, SIGNAL(fileSelected(QString)), directoryEdit, SLOT(setText(QString)));
    connect(prioritySlider, SIGNAL(valueChanged(int)), priorityLabel, SLOT(setPriority(int)));
    connect(rejectButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(downloadButton, SIGNAL(clicked()), this, SLOT(startDownload()));

    prioritySlider->setValue(1);

    layout->addWidget(downloadButton, 3, 0, -1, 3);
    layout->addWidget(rejectButton, 3, 3, -1, 3);

    directoryEdit->setText(folder);
    dialog->setDirectory(directoryEdit->text());
}

NewDownloadWindow::~NewDownloadWindow()
{
}

void NewDownloadWindow::startDownload()
{
    prepareDownload();
    close();
}

void NewDownloadWindow::prepareDownload()
{
    QString url = urlLineEdit->text();
    QString name = url.right(url.length() - url.lastIndexOf("/") - 1);
    emit newDownload(directoryEdit->text(), name , url, prioritySlider->value());
}

void PriorityLabel::setPriority(int p)
{
    const char * name = "";
    switch(p){
    case 0:name = "Low";break;
    case 1:name = "Middle";break;
    case 2:name = "High";break;
    }
    setText(name);
}

PriorityLabel::PriorityLabel()
{
    setAlignment(Qt::AlignCenter);
}


